package btree.base.traverse;

/**
 * 完整的中序遍历测试代码
 */
public class InorderTest {

    /* ===== 1. 节点定义 ===== */
    static class Node {
        int val;
        Node left, right;
        Node(int v) { val = v; }
    }

    /* ===== 2. 中序遍历 ===== */
    static void inorder(Node root, StringBuilder out) {
        if (root == null) return;
        inorder(root.left, out);
        // 记录当前节点值，用空格分隔
        out.append(root.val).append(' ');
        inorder(root.right, out);
    }

    /* ===== 3. 构造一棵树并测试 =====
     *        1
     *      /   \
     *     2     3
     *    / \   /
     *   4   5 6
     *
     * 期望中序结果：4 2 5 1 6 3
     */
    public static void main(String[] args) {
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.left = new Node(6);

        StringBuilder sb = new StringBuilder();
        inorder(root, sb);
        String actual = sb.toString().trim();
        String expected = "4 2 5 1 6 3";

        // 简单断言，失败抛异常
        if (!actual.equals(expected)) {
            throw new AssertionError("Expected: " + expected + ", but was: " + actual);
        }
        System.out.println("Inorder OK: " + actual);
    }
}
